package com.michale.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.constants.UserConstant;
import com.michale.common.domain.system.ConfigEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.system.service.ConfigService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Michale
 * @apiNote 参数管理
 */
@Api(tags = "参数管理")
@RestController
@RequestMapping("system/config")
public class ConfigController extends BaseController {

    @Autowired
    private ConfigService configService;

    /**
     * 分页查询-参数列表
     *
     * @param configEntity 分页查询条件
     * @return 参数配置列表
     */
    @GetMapping("/configList")
    @PreAuthorize("@security.isPermission('system:config:list')")
    public PageResult getDictList(ConfigEntity configEntity) {
        startPage();
        List<ConfigEntity> configList= configService.selectConfigList(configEntity);
        PageInfo<ConfigEntity> pageInfo = new PageInfo<>(configList);
        return pageResult(pageInfo);
    }

    /**
     * 根据参数编号获取详细信息
     *
     * @param configId 参数配置ID
     * @return 配置实体
     */
    @GetMapping("/{configId}")
    @PreAuthorize("@security.isPermission('system:config:query')")
    public ConfigEntity getConfigInfo(@PathVariable Long configId) {
        return configService.selectConfigById(configId);
    }

    /**
     * 根据参数键名查询参数值
     *
     * @param configKey 参数键名
     * @return 结果
     */
    @GetMapping("/configKey/{configKey}")
    @PreAuthorize("@security.isPermission('system:config:remove')")
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     *
     * @param configEntity 参数配置信息
     * @return 结果
     */
    @PostMapping("/insertConfig")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('system:config:add')")
    public AjaxResult insertConfig(@Validated @RequestBody ConfigEntity configEntity) {
        if (UserConstant.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(configEntity))) {
            return AjaxResult.error("新增参数'" + configEntity.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(configService.insertConfig(configEntity));
    }

    /**
     * 修改参数配置
     *
     * @param configEntity 参数配置信息
     * @return 结果
     */
    @PutMapping("/updateConfig")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('system:config:edit')")
    public AjaxResult updateConfig(@Validated @RequestBody ConfigEntity configEntity) {
        if (UserConstant.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(configEntity))) {
            return AjaxResult.error("修改参数'" + configEntity.getConfigName() + "'失败，参数键名已存在");
        }
        return toAjax(configService.updateConfig(configEntity));
    }

    /**
     * 根据参数配置ID删除配置信息
     *
     * @param configIds 需要删除的参数配置ID
     * @return 结果
     */
    @DeleteMapping("/deleteConfig/{configIds}")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('system:config:remove')")
    public AjaxResult deleteConfig(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return success("删除成功");
    }

    /**
     * 刷新参数缓存
     *
     * @return 结果
     */
    @DeleteMapping("/refreshCache")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('system:config:remove')")
    public AjaxResult refreshCache() {
        configService.resetConfigCache();
        return success("刷新参数缓存");
    }
}
